package main

import (
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/widget"
	"github.com/zhaocy/fyne-theme"
)

func main1() {
	myApp := app.New()
	myWin := myApp.NewWindow("app")

	exitBtn := widget.NewButton("Quit", func() {
		myApp.Quit()
	})
	myWin.SetContent(exitBtn)
	myWin.Resize(fyne.NewSize(550, 450))

	myWin.SetCloseIntercept(func() {
		myApp.Quit()
		fmt.Println("exited")
	})
	myWin.ShowAndRun()
	// myWin.Show()
	// myApp.Run()
	// quite()
}

func quite() {
	fmt.Println("exited")
}

var quitSignalChan chan os.Signal

func main() {
	myApp := app.New()
	myWin := myApp.NewWindow("app")
	myApp.Settings().SetTheme(themex.Themex())

	quitSignalChan = make(chan os.Signal, 1)
	setupCloseHandler(quitSignalChan)

	lblMsg := widget.NewLabel("观察窗口关闭后的日志")

	// 主窗口关闭时，会向通道发送关闭信号
	myWin.SetOnClosed(func() {
		quitSignalChan <- syscall.SIGQUIT
	})

	myWin.SetContent(lblMsg)
	myWin.Resize(fyne.NewSize(550, 450))
	myWin.ShowAndRun()
}

func setupCloseHandler(c chan os.Signal) {
	signal.Notify(quitSignalChan,
		os.Interrupt,
		syscall.SIGTERM,
		syscall.SIGQUIT,
		syscall.SIGINT,
	)

	go func() {
		<-c
		cleanup()
	}()
}

func cleanup() {
	//比如做一些清理工作
	fmt.Println("close log")
	fmt.Println("close conn")
}
